import DList from "@/components/dlist";
export default {
  props: {
    meal_show: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: ''
    },
    // 当前套餐
    currentPackage: {
      type: null,
      default: () => {}
    }
  },
  data() {
    return {
      package_value: "",
      package_list: []
    };
  },
  mounted() {
    this.package_value = this.currentPackage && this.currentPackage.other_data ? this.currentPackage.other_data.value : this.currentPackage && this.currentPackage.value ? this.currentPackage.value : "";
    this.getBuyPackage();
  },
  methods: {
    cancal() {
      console.log(this.type,'this.type');
      if(this.currentPackage || this.type == "change") {
        this.$emit("update:meal_show", false);
      }else {
        this.surePackage();
      }
    },
    // 企业-更换套餐选择页
    getBuyPackage() {
      $http.get("plugin.be-within-call.frontend.buy-package.index", {  }).then(({ data , result , msg }) => {
        if (result) {
          this.package_list = data.package_list;
          this.package_value = this.package_value ? this.package_value : this.package_list.length > 0 ? this.package_list[0].value : "";
        } else {
          this.$toast(msg);
        }
      });
    },
    surePackage() {
      this.$dialog.confirm({
        title: '提示',
        message: '更换套餐后，之前的套餐将失效，是否确认更换?',
      })
        .then(async () => {
          let packageArr = this.package_list.filter(item => item.value == this.package_value);
          $http.get("plugin.be-within-call.frontend.buy-package.change", { package_type: packageArr[0].value }).then(({ data , result , msg }) => {
            console.log(data , result , msg ,5236);
            if (result) {
              if(data.need_pay == 1) {
                this.$router.push(
                  this.fun.getUrl("orderpay", {
                    status: 2,
                    order_ids: data.order_id
                  })
                );
              }else {
                this.$toast(msg);
                this.$emit('optionPackage',packageArr)
              }
            } else {
              this.$toast(msg);
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    }
  },
  components: { DList }
};
